#include "stdafx.h"
#include "buffer_simple.h"
#include <math.h>

void buffer_simple_initial(void * buffer)
{
	buffer_simple_t * bs = (buffer_simple_t *)buffer;
	bs->get_line_func = buffer_simple_get_line;
	bs->aa_set_pixel_func = buffer_simple_set_pixel_aa;
	bs->na_set_pixel_func = buffer_simple_set_pixel_na;
	bs->gamma = 1.0f;
	for(uint_x cnt = 0; cnt != 256; ++cnt)
	{
		bs->gmap[cnt] = (uint_8)cnt;
	}
}
void buffer_simple_set_gama(void * buffer, float_32 gamma)
{
	buffer_simple_t * bs = (buffer_simple_t *)buffer;
	bs->gamma = gamma;
	uint_32 gmap = 0;
	for(uint_x cnt = 0; cnt != 256; ++cnt)
	{
		gmap = (uint_32)(powf((float_32)cnt / 255.0f, gamma) * 255.0f);
		if(gmap > 0xFF)
			bs->gmap[cnt] = 0xFF;
		else
			bs->gmap[cnt] = (uint_8)gmap;
	}
}
uint_8 * buffer_simple_get_line(tag_buffer * buffer, int_32 y)
{
	buffer_simple_t * bs = (buffer_simple_t *)buffer;
	if(y < 0 || y >= int_32(bs->img.height))
		return 0;
	else
		return image_get_line(&bs->img, y);
}

void buffer_simple_set_pixel_aa(tag_buffer * buffer, uint_8 * line, int_32 x, int_32 alpha)
{
	buffer_simple_t * bs = (buffer_simple_t *)buffer;
	if(x < 0 || x >= int_32(bs->img.width))
		return ;
	else
	{
		alpha = bs->gmap[alpha];
		uint_32 color = (alpha << 24) | (bs->color & 0x00FFFFFF);
		color_argb_blend(line + (x << 2), &color);
	}
}

void buffer_simple_set_pixel_na(tag_buffer * buffer, uint_8 * line, int_32 x)
{
	buffer_simple_t * bs = (buffer_simple_t *)buffer;
	uint_32 color = 0xFF000000 | bs->color;
	color_argb_blend(line + (x << 2), &color);
}